package com.alex.algorithm.easy;

public class SumRootToNodeNumbers {

    public static void main(String[] args) {
        BinaryTreeNode root = new BinaryTreeNode();
        root.setValue(1);

        BinaryTreeNode n1 = new BinaryTreeNode();
        n1.setValue(1);
        root.setLeft(n1);

        BinaryTreeNode n2 = new BinaryTreeNode();
        n2.setValue(2);
        root.setRight(n2);

        BinaryTreeNode n3 = new BinaryTreeNode();
        n3.setValue(3);

        n2.setRight(n3);

        BinaryTreeNode n4 = new BinaryTreeNode();
        n4.setValue(3);
        n3.setRight(n4);

        int val = 0;
        System.out.println(countNode(root, val));
    }

    private static int countNode(BinaryTreeNode node, int val) {
        int ret = 0;
        if (node == null) {
            return ret;
        }
        ret += node.getValue() + val * 10;
        if(node.getLeft() == null && node.getRight() == null){
            return ret;
        }
        int l = countNode(node.getLeft(), ret);
        int r = countNode(node.getRight(), ret);
        return l + r;
    }
}
